package com.xn.app.muying.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xn.app.muying.R;
import com.xn.app.muying.activity.Check_KePu_Activity;
import com.xn.app.muying.adpter.HomeKePuAdapter;
import com.xn.app.muying.bean.HomeKePuBean;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/14.
 */
public class KePuWomanFragment extends Fragment implements HttpUtil.OnGetDataListener {
    private Handler handler = new Handler();
    private ArrayList<HomeKePuBean> list;
    private HomeKePuAdapter adapter;
    private HomeKePuBean bean;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.kepu_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView lv = (ListView) view.findViewById(R.id.kepu_fragment_lv);
        HttpUtil.getData(APIMethod.Home_KePu, handler, this);
        adapter = new HomeKePuAdapter(getActivity(), handler);
        lv.setAdapter(adapter);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent();
                intent.putExtra("id",list.get(position).getId());
                intent.setClass(getActivity(), Check_KePu_Activity.class);
                getActivity().startActivity(intent);

            }
        });

    }

    @Override
    public void onJsonData(String data) {
        try {
            JSONObject jsonObject = new JSONObject(data);
            JSONArray array = jsonObject.getJSONArray("items");
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);
                String seoName = object.optString("seoName");
                String id = object.optString("id");
                bean = new HomeKePuBean();
                bean.setSeoName(seoName);
                bean.setId(id);
            }
            if (bean.getId().equals(1+"")) {
                HttpUtil.getData(APIMethod.Home_KePu2(bean.getSeoName()), handler, new HttpUtil.OnGetDataListener() {
                    @Override
                    public void onJsonData(String data) {
                        Log.i("走不走",data.toString());
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            JSONArray array = jsonObject.getJSONArray("items");
                            list = new ArrayList<>();
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject object = array.getJSONObject(i);
                                String id = object.optString("id");
                                String name = object.optString("name");
                                String title = object.optString("title");
                                String pictureUrl = object.optString("pictureUrl");
                                String content = object.optString("content");

                                bean = new HomeKePuBean();
                                bean.setId(id);
                                bean.setName(name);
                                bean.setTitle(title);
                                bean.setPictureUrl(pictureUrl);
                                bean.setContent(content);
                                list.add(bean);

                            }
                            adapter.getData(list);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                });
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
